package com.mystudy.atomic.reference;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicReference;

/**
 * todo:
 *
 * @Author acy
 * @Date 2019/06/05
 */
public class AtomicReferenceTest {
	@Test
	public void test() throws Exception{
		AtomicReference<Simple> reference = new AtomicReference<>(new Simple("acy",1314));
		System.out.println(reference.get());
		Simple szh = new Simple("szh", 25);
		AtomicReference<Simple> atomicReference = new AtomicReference<>(szh);
		System.out.println(atomicReference.get());
		atomicReference.compareAndSet(new Simple("szh",25),new Simple("cy",23));
		System.out.println(atomicReference.get());
		atomicReference.compareAndSet(szh,new Simple("cy",23));
		System.out.println(atomicReference.get());
	}


	static class Simple{
		private String name;
		private int age;

		public Simple(String name, int age) {
			this.name = name;
			this.age = age;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		@Override
		public String toString() {
			return "Simple{" +
					"name='" + name + '\'' +
					", age=" + age +
					'}';
		}
	}
}
